package com.jhc.drone.communication.vo;


import com.jhc.drone.communication.enums.DirectionEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @author lcp
 * @date 2025年02月2025/2/6 14:25
 * @describe
 */
@Data
public class RcVo implements Serializable {

    private static final float MIN_UPPER = (float) ((1920 - 1480) / 3);

    private static final float MIN_LOWER = (float) ((1480 - 1040) / 3);

    //水平速度（0 m/s - 3 m/s）
    private float levelSpeed;

    //垂直速度（0 m/s - 3 m/s）
    private float verticalSpeed;

    // FRONT-前，BACK-后，LEFT-左，RIGHT-右
    private DirectionEnum levelDir;

    // UP-上升，DOWN-下降
    private DirectionEnum verticalDir;

    // LEFT 无人机左边旋转姿态，RIGHT 无人机右边旋转姿态
    private DirectionEnum direction;

    // 姿态偏转速度
    private float deflection;

    public Integer getChannel1() {
        if (levelDir == null) {
            return 1480;
        }
        if (levelDir == DirectionEnum.LEFT) {
            return getLogicValue(levelSpeed, false);
        }
        if (levelDir == DirectionEnum.RIGHT) {
            return getLogicValue(levelSpeed, true);
        }
        return 1480;
    }

    public Integer getChannel2() {
        if (levelDir == null) {
            return 1480;
        }
        if (levelDir == DirectionEnum.BACK) {
            return getLogicValue(levelSpeed, false);
        }
        if (levelDir == DirectionEnum.FRONT) {
            return getLogicValue(levelSpeed, true);
        }
        return 1480;
    }

    public Integer getChannel3() {
        if (verticalDir == null) {
            return 1480;
        }
        if (verticalDir == DirectionEnum.DOWN) {
            return getLogicValue(verticalSpeed, false);
        }
        if (verticalDir == DirectionEnum.UP) {
            return getLogicValue(verticalSpeed, true);
        }
        return 1480;
    }

    public Integer getChannel4() {
        if(direction == null) {
            return 1480;
        }
        if(direction ==  DirectionEnum.LEFT){
            return getLogicValue(deflection, false);
        }
        if(direction ==  DirectionEnum.RIGHT){
            return getLogicValue(deflection, true);
        }
        return 1480;
    }

    // 1040-1500-1920
    private static Integer getLogicValue(float speed, Boolean direction) {
        if (speed <= 0) {
            return 1480;
        }
        if (speed > 3) {
            return 1920;
        }
        if (direction) {
            return 1480 + (int) (speed * MIN_UPPER);
        } else {
            return 1480 - (int)(speed * MIN_LOWER);
        }
    }

}
